package edu.gatech.cs2340.thecyclonesquad;

import edu.gatech.cs2340.thecyclonesquad.database.Database;
import android.os.Build;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class MatchPageActivity extends Activity {
	
	private ArrayAdapter<String> mListData12;

	/**
	 * Initializes our activity.
	 */
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_match_page);
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            // Show the Up button in the action bar.
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }
	}

	/**
	 * Initialize the contents of the Activity's standard options menu.
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_match_page, menu);
		return true;
	}
	
	/**
	 * When item lost entered, will display matched found items. 
	 * @param view is the basic building block for UI components
	 */
	public void itemMatch(View view) {
		ListView lvList12 = (ListView)findViewById(R.id.listView12);
		EditText editText = (EditText) findViewById(R.id.lostItemName);
		String name = editText.getText().toString();
		Database entry = new Database(this);
    	entry.open();
    	String items[] = entry.matchName(name);
    	entry.close();
    	if (items.length == 0) {
    		String items2[] = new String[1];
    		items2[0] = "No match found!";
    		mListData12 = new ArrayAdapter<String>(MatchPageActivity.this, android.R.layout.simple_list_item_1, items2);
    	} else {
    		mListData12 = new ArrayAdapter<String>(MatchPageActivity.this, android.R.layout.simple_list_item_1, items);
    	}
        //Set adapter to the list
        lvList12.setAdapter(mListData12);
		
	}
	
	/**
	 * The logOut method logs the user out and goes back to login page.
	 * @param view is the basic building block for UI components
	 */
	public void logOut(View view) {
		Intent intent = new Intent(this, SignInActivity.class);
		startActivity(intent);			
	}

}
